package com.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间日期工具
 * @author dell
 *
 */
public class DateUtil {
	
	public static String formatNowDateTime(String strFormat) {
		Date basicDate = new Date();
		SimpleDateFormat df = new SimpleDateFormat(strFormat);
		return df.format(basicDate);
	}
	/**
	 * <li>@方法说明:相隔天数
	 * <li>@param firstString
	 * <li>@param secondString
	 * <li>@param datafmt
	 * <li>@return
	 * <li>@创建人:王剑
	 * <li>@创建日期:2010-5-4
	 * <li>@修改人: 
	 * <li>@修改日期:
	 * <li>@详细说明: 
	 */
	public static int nDaysBetweenTwoDate(String firstString,String secondString,String datafmt) { 
		SimpleDateFormat df = new SimpleDateFormat(datafmt); 
		Date firstDate=null; 
		Date secondDate=null; 
		try { 
		firstDate = df.parse(firstString); 
		secondDate=df.parse(secondString); 
		} 
		catch(Exception e) { 
		// 日期型字符串格式错误 
		} 
		
		int nDay=(int)((secondDate.getTime()-firstDate.getTime())/(24*60*60*1000)); 
		return nDay; 
		} 
	public static Date nDaysAfterOneDate(Date basicDate, int n) {
		long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
				* (24 * 60 * 60 * 1000);
		basicDate.setTime(nDay);

		return basicDate;
	}
	public static String nDaysAfterOneDateString(String basicDate,int n,String datafmt) { 
		SimpleDateFormat df = new SimpleDateFormat(datafmt); 
		Date tmpDate = null; 
		try { 
		tmpDate = df.parse(basicDate); 
		} 
		catch(Exception e){ 
		// 日期型字符串格式错误 
		} 
		long nDay=(tmpDate.getTime()/(24*60*60*1000)+1+n)*(24*60*60*1000); 
		tmpDate.setTime(nDay); 
		
		return df.format(tmpDate); 
		} 
	
	public static String formatDateTime(Date basicDate, String strFormat) {
		if (basicDate == null) {
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat(strFormat);
		return df.format(basicDate);
	}
	
	public static String formatDateTime(String basicDate, String oldFormat,
			String newFormat) throws Exception {
		if (basicDate == null || "".equals(basicDate)) {
			return "";
		}
		SimpleDateFormat odf = new SimpleDateFormat(oldFormat);
		SimpleDateFormat ndf = new SimpleDateFormat(newFormat);
		Date tmpDate = null;
		try {
			tmpDate = odf.parse(basicDate);
		} catch (Exception e) {
			throw e;
		}
		return ndf.format(tmpDate);
	}

	public static Date getDate(String basicDate, String oldFormat) throws Exception{
		if (basicDate == null || "".equals(basicDate)) {
			return null;
		}
		SimpleDateFormat odf = new SimpleDateFormat(oldFormat);
		Date tmpDate = null;
		try {
			tmpDate = odf.parse(basicDate);
		} catch (Exception e) {
			throw e;
		}
		return tmpDate;
	}

	public static String getNowDate(String fmtStr){
		Date d = new Date();
		SimpleDateFormat odf = new SimpleDateFormat(fmtStr);
		return odf.format(d);
	}
	
	public static String getDateTime2(String basicDate) {
		try {
			if (basicDate.trim().length() >= 14) {
				return formatDateTime(basicDate.substring(0, 14),
						"yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss");
			} else if (basicDate.trim().length() >= 8
					&& basicDate.trim().length() < 14) {
				return formatDateTime(basicDate.substring(0, 8), "yyyyMMdd",
						"yyyy-MM-dd");
			}
		} catch (Exception e) {
			return "";
		}
		return "";
	}
	
	/**
	 * <li>@方法说明:
	 * <li>@param month  yyyy-MM
	 * <li>@return
	 * <li>@创建人:王剑
	 * <li>@创建日期:2010-4-21
	 * <li>@修改人: 
	 * <li>@修改日期:
	 * <li>@详细说明: 
	 */
	public static int getMonthDays(String month){
		Calendar time=Calendar.getInstance(); 
		time.clear(); 
		time.set(Calendar.YEAR,Integer.parseInt(month.substring(0, 4))); 
		time.set(Calendar.MONTH,Integer.parseInt(month.substring(5))-1);//Calendar对象默认一月为0            
		int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
		return day;
	}
	
}
